package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.CraftStepImg;
import com.uwlaser.service.CraftStepImgService;
import com.uwlaser.service.dto.CraftStepImgDto;
import com.uwlaser.service.dto.CraftStepImgQueryParam;
import com.uwlaser.service.mapper.CraftStepImgMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = CraftStepImgService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CraftStepImgServiceImpl extends CommonServiceImpl<CraftStepImgMapper, CraftStepImg> implements CraftStepImgService {

    // private final RedisUtils redisUtils;
    private final CraftStepImgMapper craftStepImgMapper;

    @Override
    public PageInfo<CraftStepImgDto> queryAll(CraftStepImgQueryParam query, Pageable pageable) {
        IPage<CraftStepImg> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<CraftStepImg> page = craftStepImgMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, CraftStepImgDto.class);
    }

    @Override
    public List<CraftStepImgDto> queryAll(CraftStepImgQueryParam query){
        return ConvertUtil.convertList(craftStepImgMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), CraftStepImgDto.class);
    }

    @Override
    public CraftStepImg getById(Integer id) {
        return craftStepImgMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public CraftStepImgDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), CraftStepImgDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CraftStepImgDto resources) {
        CraftStepImg entity = ConvertUtil.convert(resources, CraftStepImg.class);
        return craftStepImgMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(CraftStepImgDto resources){
        CraftStepImg entity = ConvertUtil.convert(resources, CraftStepImg.class);
        int ret = craftStepImgMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return craftStepImgMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<CraftStepImgDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (CraftStepImgDto craftStepImg : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("工步ID", craftStepImg.getStepId());
              map.put("服务器路径", craftStepImg.getServerPath());
              map.put("访问路径", craftStepImg.getVisitPath());
              map.put("原文件名", craftStepImg.getOriginalFileName());
              map.put("文件名", craftStepImg.getFileName());
              map.put("文件后缀", craftStepImg.getSuffix());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
